﻿Imports SistemaINCA.Security
Imports SistemaINCA.Models.ViewModels

Namespace Models

    Public Interface IGrados
        '<Authorize(Roles:="ERA")> _
        'Function GetListGrados(Año As String) As List(Of GRADO)

        '<Authorize(Roles:="ERA")> _
        'Function GetListGrados_FormatModel(Año As String) As List(Of GradoModel)

        '<Authorize(Roles:="ERA")> _
        'Function GetSegmentListGrados(Año As String, index As Integer, Size As Integer) As List(Of GradoModel)

        '<Authorize(Roles:="ERA")> _
        'Function InsertNewGrado(AñoEscolar As String, AñoGrado As String, Especialidad As String, Seccion As String, Cantidad As Integer, ByRef msgError As String) As Boolean

        '<Authorize(Roles:="ERA")> _
        'Function CountGrados(Año As String) As Integer

        '<Authorize(Roles:="ERA")> _
        'Function DeleteGrado(ID As String, ByRef msgError As String) As Boolean

        '<Authorize(Roles:="ERA")> _
        'Function UpdateGrado(ID As String, Grado As String, Especialidad As String, Seccion As String, ByRef msgError As String) As Boolean

        '<Authorize(Roles:="ERA")> _
        'Function GetGrados_ByGroupAndAnio_FormatCheckList(IDGroup As Integer, Año As String) As Dictionary(Of String, String)

        '<Authorize(Roles:="ERA")> _
        'Function GetGrados_ByGroupAndAnio(IDGroup As Integer, Año As String) As List(Of GET_LIST_GRADOS_BYGROUP_AND_ANIO_DATATYPE)

    End Interface

    Partial Public Class SchoolClickDB_Repositorio
        Implements IGrados

        Public ReadOnly Property IGrados() As IGrados
            Get
                Return DirectCast(Me, IGrados)
            End Get
        End Property

        'Private Function GetListGrados(Año As String) As System.Collections.Generic.List(Of GRADO) Implements IGrados.GetListGrados
        '    Using cnn As New SchoolClickEntities
        '        Try
        '            Return cnn.GRADOS.Where(Function(g) g.ANO_LECTIVO = Año) _
        '                                .OrderBy(Function(m) m.ANO_BACHILLERATO) _
        '                                .ThenByDescending(Function(g) g.ESPECIALIDAD) _
        '                                .ThenBy(Function(g) g.SECCION) _
        '                                .ToList
        '        Catch ex As Exception
        '            Throw New ApplicationException(String.Format("Lo sentimos, No se pudo obtener el listado de grados del año {0}. {1}", Año, ex.Message))
        '        End Try
        '    End Using
        'End Function

        'Private Function InsertNewGrado(AñoEscolar As String, AñoGrado As String, Especialidad As String, Seccion As String, Cantidad As Integer, ByRef msgError As String) As Boolean Implements IGrados.InsertNewGrado
        '    Dim eCompleted As Boolean = False
        '    Using cnn As New SchoolClickEntities
        '        Try
        '            Dim bResult = cnn.INS_GRADO(AñoEscolar, AñoGrado, Especialidad, Seccion, Cantidad, _
        '                                   EncryptationSchoolClick.ToStringInseguro(EncryptationSchoolClick.DesencriptarData(My.Resources.PasswordsEncrypts.ERA_ROL))).Single

        '            If bResult Then
        '                eCompleted = True
        '            Else
        '                msgError = "Lo sentimos, no hemos podido insertar el o los nuevos grados en el sistema"
        '            End If
        '        Catch ex As EntityCommandExecutionException
        '            msgError = ex.InnerException.Message
        '        Catch ex As Exception
        '            'OPTIONAL: Si existiera un error durante toda la operacion, unicamente la interceptamos y la ignoramos.
        '            'Solo tomaremos el mensaje del error y lo regresaremos en la variable delo mensaje de error
        '            msgError = FormatErrorMessage("Ocurrio un error grave en la operacion.", ex.Message)
        '        End Try
        '    End Using
        '    Return eCompleted
        'End Function

        'Private Function GetListGrados_FormatModel(Año As String) As System.Collections.Generic.List(Of ViewModels.GradoModel) Implements IGrados.GetListGrados_FormatModel
        '    Try
        '        Dim items = New List(Of GradoModel)
        '        For Each g In Me.GetListGrados(Año)
        '            items.Add(New GradoModel With {.ID = g.ID_GRADO, .AnioEscolar = g.ANO_LECTIVO, .Grado = g.ANO_BACHILLERATO, _
        '                                           .Especialidad = g.ESPECIALIDAD, .Seccion = g.SECCION})
        '        Next
        '        Return items
        '    Catch ex As Exception
        '        Throw New ApplicationException(String.Format("Lo sentimos, No se pudo obtener el listado de grados del año {0}. {1}", Año, ex.Message))
        '    End Try
        'End Function

        'Private Function CountGrados(Año As String) As Integer Implements IGrados.CountGrados
        '    Using cnn As New SchoolClickEntities
        '        Try
        '            Return cnn.GRADOS.Where(Function(g) g.ANO_LECTIVO = Año).Count
        '        Catch ex As Exception
        '            Throw New ApplicationException(String.Format("Lo sentimos, No se pudo recuperar la cantidad de registros para el año {0}. {1}", Año, ex.Message))
        '        End Try
        '    End Using
        'End Function

        'Private Function GetSegmentListGrados(Año As String, index As Integer, Size As Integer) As System.Collections.Generic.List(Of ViewModels.GradoModel) Implements IGrados.GetSegmentListGrados
        '    Using cnn As New SchoolClickEntities
        '        Try
        '            Dim var = cnn.GRADOS.Where(Function(g) g.ANO_LECTIVO = Año) _
        '                             .OrderBy(Function(m) m.ANO_BACHILLERATO) _
        '                             .ThenByDescending(Function(g) g.ESPECIALIDAD) _
        '                             .ThenBy(Function(g) g.SECCION) _
        '                             .Skip(index) _
        '                             .Take(Size).ToList

        '            Return var.Select(Function(g) New GradoModel With {.ID = g.ID_GRADO, .AnioEscolar = g.ANO_LECTIVO, .Grado = g.ANO_BACHILLERATO, _
        '                                           .Especialidad = g.ESPECIALIDAD, .Seccion = g.SECCION})
        '        Catch ex As Exception
        '            Throw New ApplicationException(String.Format("Lo sentimos, No se pudo obtener el listado de grados del año {0}. {1}", Año, ex.Message))
        '        End Try
        '    End Using
        'End Function

        'Private Function DeleteGrado(ID As String, ByRef msgError As String) As Boolean Implements IGrados.DeleteGrado
        '    Dim eCompleted As Boolean = False
        '    Using cnn As New SchoolClickEntities
        '        Try
        '            Dim bResult = cnn.DEL_GRADO(ID, EncryptationSchoolClick.ToStringInseguro(EncryptationSchoolClick.DesencriptarData(My.Resources.PasswordsEncrypts.ERA_ROL))).Single()
        '            If bResult Then
        '                eCompleted = True
        '            Else
        '                msgError = "No se puede eliminar el grado del sistema"
        '            End If
        '        Catch ex As EntityCommandExecutionException
        '            msgError = ex.InnerException.Message
        '            ex.Data.Clear()
        '        Catch ex As Exception
        '            'OPTIONAL: Si existiera un error durante toda la operacion, unicamente la interceptamos y la ignoramos.
        '            'Solo tomaremos el mensaje del error y lo regresaremos en la variable delo mensaje de error
        '            msgError = FormatErrorMessage("Ocurrio un error inesperado al realizar la operacion", ex.Message)
        '            ex.Data.Clear()
        '        End Try
        '    End Using
        '    Return eCompleted
        'End Function

        'Private Function UpdateGrado(ID As String, Grado As String, Especialidad As String, Seccion As String, ByRef msgError As String) As Boolean Implements IGrados.UpdateGrado
        '    Dim eCompleted As Boolean = False
        '    Using cnn As New SchoolClickEntities
        '        Try
        '            Dim bResult = cnn.UPT_GRADO(ID, Grado, Especialidad, Seccion.ToUpper, _
        '                                        EncryptationSchoolClick.ToStringInseguro(EncryptationSchoolClick.DesencriptarData(My.Resources.PasswordsEncrypts.ERA_ROL))).Single()
        '            If bResult Then
        '                eCompleted = True
        '            Else
        '                msgError = "No se pudo actualizar el grado en el sistema"
        '            End If
        '        Catch ex As EntityCommandExecutionException
        '            msgError = ex.InnerException.Message
        '        Catch ex As Exception
        '            'OPTIONAL: Si existiera un error durante toda la operacion, unicamente la interceptamos y la ignoramos.
        '            'Solo tomaremos el mensaje del error y lo regresaremos en la variable delo mensaje de error
        '            msgError = FormatErrorMessage("Ocurrio un error inesperado al realizar la operacion", ex.Message)
        '            ex.Data.Clear()
        '        End Try
        '    End Using
        '    Return eCompleted
        'End Function

        'Private Function GetGrados_ByGroupAndAnio_FormatCheckList(IDGroup As Integer, Año As String) As Dictionary(Of String, String) Implements IGrados.GetGrados_ByGroupAndAnio_FormatCheckList
        '    Using cnn As New SchoolClickEntities
        '        Try
        '            Dim data = cnn.GET_LIST_GRADOS_BYGROUP_AND_ANIO(Año, IDGroup).ToList
        '            Dim dictionary As New Dictionary(Of String, String)

        '            For Each g In data
        '                dictionary.Add(g.ID_GRADO, g.NAMEGRADO)
        '            Next

        '            Return dictionary
        '        Catch ex As Exception
        '            Throw New ApplicationException(String.Format("Lo sentimos, No se pudo obtener el listado de grados del año {0}. {1}", Año, ex.Message))
        '        End Try
        '    End Using
        'End Function

        'Private Function GetGrados_ByGroupAndAnio(IDGroup As Integer, Año As String) As System.Collections.Generic.List(Of GET_LIST_GRADOS_BYGROUP_AND_ANIO_DATATYPE) Implements IGrados.GetGrados_ByGroupAndAnio
        '    Using cnn As New SchoolClickEntities
        '        Try
        '            Return cnn.GET_LIST_GRADOS_BYGROUP_AND_ANIO(Año, IDGroup).OrderBy(Function(g) g.SECCION).ToList
        '        Catch ex As Exception
        '            Throw New ApplicationException(String.Format("Lo sentimos, No se pudo obtener el listado de grados del año {0}. {1}", Año, ex.Message))
        '        End Try
        '    End Using
        'End Function
    End Class
End Namespace
